import cryptoJS from 'crypto-js'

const SECRET_KEY = cryptoJS.enc.Utf8.parse('47FC4124CF94E070') // 密钥
const SECRET_IV = cryptoJS.enc.Utf8.parse('ovOh2xYoRdfATob6') // 密钥偏移量

export default {
  /**
   * 加密方法
   * @param data
   * @returns {string}
   */
  encrypt(data: any) {
    const dataHex = cryptoJS.enc.Utf8.parse(data)
    const encrypted = cryptoJS.AES.encrypt(dataHex, SECRET_KEY, {
      iv: SECRET_IV,
      mode: cryptoJS.mode.CBC,
      padding: cryptoJS.pad.Pkcs7
    })
    return encrypted.ciphertext.toString()
  },

  /**
   * 解密方法
   * @param data
   * @returns {string}
   */
  decrypt(data: any) {
    const encryptedHexStr = cryptoJS.enc.Hex.parse(data)
    const str = cryptoJS.enc.Base64.stringify(encryptedHexStr)
    const decrypt = cryptoJS.AES.decrypt(str, SECRET_KEY, {
      iv: SECRET_IV,
      mode: cryptoJS.mode.CBC,
      padding: cryptoJS.pad.Pkcs7
    })
    const decryptedStr = decrypt.toString(cryptoJS.enc.Utf8)

    return decryptedStr.toString()
  }
}
